package 笔试2.pdd.four;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/*

2
3 4 3 3
1 2 3
1 1
1 3
2 2
3 1
0 1 1 1
1 1

 */
//按指令走，输出最终坐标
public class shuru {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        List<int[]> csList= new ArrayList<>();
        List<List<Integer>> zhilingList = new ArrayList<>();
        List<List<int[]>> xyList = new ArrayList<>();
        for (int i = 0; i < T; i++) {

            int[] cs = new int[4];
            for (int j = 0; j < 4; j++) {
                cs[j]=sc.nextInt();
            }
            csList.add(cs);

            List<Integer> tmp = new ArrayList<>();
            for (int j = 0; j < cs[0]; j++) {
                tmp.add(sc.nextInt());
            }
            zhilingList.add(tmp);

            List<int[]> kb= new ArrayList<>();
            for (int j = 0; j < cs[1]; j++) {
                int[] xy = {sc.nextInt(), sc.nextInt()};
                kb.add(xy);
            }
            xyList.add(kb);


        }

        for (int i = 0; i < T; i++) {
            int[] cs = csList.get(i);
            List<Integer> zl = zhilingList.get(i);
            List<int[]> xy = xyList.get(i);
            solve(cs,zl,xy);

        }



    }

    static int[][] g;
    private static void solve(int[] cs, List<Integer> zl, List<int[]> xy) {
        int m=cs[2],n=cs[3];
        g=new int[m][n];

        for (int[] ints : xy) {
            g[ints[0]-1][ints[1]-1]=1;
        }
        //找起点(若固定00起，删掉)
        for (int i=0;i<g.length;i++) {
            for (int j=0;j<g[0].length;j++) {
                if(g[i][j]==1){
                    int[] z = new int[zl.size()];
                    for (int k = 0; k < zl.size(); k++) {
                        z[k]=zl.get(k);
                    }
                    qian(i,j,z,0);
                }
            }
        }
    }


    static void qian(int i, int j, int[] zl,int k) {
        if(k==zl.length){
            int resi=i+1,resj=j+1;
            System.out.println(resi+" "+resj);
//            System.out.println();
            return;
        }
        int cZL = zl[k];

        int x=i+fx[cZL-1][0],y=j+fx[cZL-1][1];

        //停止
        if(x<0||x>=g.length||y<0||y>=g[0].length //越界
                || g[x][y]==6  //vis走过
                || g[x][y]==-1 //障碍
        ) {

            qian(i, j, zl, k + 1);
        }
        else {

            //other全都能走 (con排-1 6 return排2 -> 就剩’0‘)
            step++;
            int t=g[x][y];
            g[x][y] = 6;
            qian(x, y, zl, k + 1);
            step--;
            g[x][y] = t;
        }

    }

//    static int[][] g = {
//            //1起点 -1障碍 6走过 2终点 0可走
//            //停止:-1 6
//            //return: 2
//            //可走 (0)
//
//            {1,0,1},
//            {0,1,0},
//            {1,0,0},
//    };
    static int[][] G ;
    static int[][] fx = {{-1,0},{1,0},{0,-1},{0,1}};

    static int step=0;
    static int resMin=9999;
    static void ptIntShuzu(int[][] p){
        for (int i = 0; i < p.length; i++) {
            for (int j = 0; j < p[0].length; j++) {
                System.out.print(p[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("——————————————————————————");
    }
}
